package com.bk.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.bk.utils.BKBookingUtils;
import com.jfinal.plugin.activerecord.Db;
import com.moomi.common.model.BkItem;
import com.moomi.common.model.BkItemSelection;
import com.moomi.common.model.BkSelection;

/**
 * 项目管理
 * 
 * @author cen
 *
 */
public class BKItemController extends BKAuthController {

	@Override
	public void index() {
		try {
			super.index();
			setTitle("服务项目管理");
			int shopId = getShopId();
			List<BkItem> items = BkItem.dao.findItemsByShopId(shopId);
			setAttr("items", items);
			render("table.html");
		} catch (Exception e) {
			e.printStackTrace();
			renderFailure();
		}
	}

	@Override
	public void update() {
		super.update();
		BkItem model = (BkItem) updateModel(BkItem.class);
		try {
			// System.out.println("getSpend: " + model.getSpend());
			// System.out.println(model.getLimit());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void turn() {
		Integer id = getParaToInt(0);
		BkItem item = BkItem.dao.findById(id);
		int visiable = item.getVisible();
		visiable = (visiable + 1) % 2;
		item.setVisible(visiable);
		renderFlag(item.update());
	}

	@Override
	public void info() {
		super.info();
		setTitle("服务项目");
		ArrayList<String> times = new ArrayList<>();
		BKBookingUtils utils = new BKBookingUtils();
		for (int i = 1; i < 9; i++) {
			times.add(utils.timeIndexToString(i));
		}
		setAttr("times", times);
		modelInfo(BkItem.class, "form.html", false);
	}

	@Override
	public void delete() {
		super.delete();
		Integer id = getParaToInt("id");
		BkItem item = BkItem.dao.findById(id);
		item.setFlag(1);
		boolean flag = item.update();
		if (flag) {
			renderSuccess();
		} else {
			renderMsg("删除失败");
		}
		// deleteModel(BkItem.class);
	}

	public void select() {
		setAttr("action", "updateSelection");
		// setAttr("list", PxlmTeacher.dao.search("schoolId", getSchoolId()));
		int id = getId();
		BkItem item = BkItem.dao.findById(id);

		List<BkSelection> AllSelections = BkSelection.dao.find(Db.getSql("AdminSelections"), getShopId());

		List<BkItemSelection> itemSelections = BkItemSelection.dao.search("itemId", id);
		ArrayList<HashMap<String, Object>> list = new ArrayList<>();
		for (BkSelection selection : AllSelections) {
			HashMap<String, Object> map = new HashMap<>();
			map.put("checked", "");
			map.put("info", selection);
			for (BkItemSelection itemSelection : itemSelections) {
				if (itemSelection.getSelectionId() == selection.getId()) {
					map.put("checked", "checked");

				}
			}
			list.add(map);
		}
		setAttr("teachers", list);
		setAttr("info", item);
		// setAttr("title", item.getName());
		render("selection.html");
	}

	public void updateSelection() {
		getFiles();
		int itemId = getId();
		List<BkItemSelection> itemSelections = BkItemSelection.dao.search("itemId", itemId);
		for (BkItemSelection bkItemSelection : itemSelections) {
			bkItemSelection.delete();
		}

		try {
			String[] values = getParaValues("selected");
			for (String string : values) {
				BkItemSelection itemSelection = new BkItemSelection();
				itemSelection.setItemId(itemId);
				itemSelection.setSelectionId(Integer.parseInt(string));
				boolean flag = itemSelection.save();

			}

		} catch (Exception e) {
		}

		renderReload();

		// redirect("/bk/item/select/?id=" + itemId);

	}
}
